package controller;

import beans.AlgorithmDetail;
import beans.Model;
import dao.AlgorithmDao;
import dao.ModelDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import portal.controller.BaseServlet;
import utils.Log4jInit;
import utils.LogOperateEnum;
import utils.LogUtil;
import utils.ParseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

public class ModelDetail extends BaseServlet {
	@Autowired
	AlgorithmDao algorithmDao;// = new AlgorithmDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Model model= (Model) new ModelDao().getModelByModelId(request.getParameter("modelId")).get(0);
        AlgorithmDetail ad=null;
        if(model.isDefault()){
        	Log4jInit.logger.info("Model ID:"+model.getModelId());
        	Log4jInit.logger.info("Algorithm ID:"+model.getAlgorithm_id());
        	ad=algorithmDao.getAlgorithmDetailWithoutUserId(model.getAlgorithm_id()+"");
        }
        		
        JSONObject modelJsonObject = JSONObject.fromObject(model.getJson());
        JSONObject resultObject = ParseUtil.parseModelDetail(modelJsonObject);
        resultObject.remove("blocks");
        resultObject.remove("lines");
        resultObject.remove("taskNumber");
        if(null!=ad)
        resultObject.put("defaultMPI", ad.getCpuNum());
        JSONObject result = new JSONObject();
        result.put("result", resultObject);
        response.getWriter().print(result.toString());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
